<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir . "/inc/database.php";

class Permission {
    
    public static $TABLENAME = "permissions";
    public static $CLASSNAME = "Permission";
    
    private $id;
    private $name;
    private $description;
    
    public static $STAMMDATEN = 'stammdaten';
    public static $STAMMDATEN_ADRESS = 'stammdaten_adress';
    public static $STAMMDATEN_BANK = 'stammdaten_bank';
    public static $STAMMDATEN_KINDER = 'stammdaten_kind';
    public static $STAMMDATEN_MITARBEITER = 'stammdaten_mitarbeiter';
    public static $STAMMDATEN_MITARBEITER_GEHAELTER = 'stammdaten_mitarbeiter_gehaelter';
    public static $STAMMDATEN_GRUPPEN = 'stammdaten_gruppen';
    public static $STAMMDATEN_KONTEN = 'stammdaten_konten';
    
    public static $VERTRAEGE = 'vertraege';
    public static $VERTRAEGE_BANKDATEN = 'vertraege_bankdaten';
    public static $VERTRAEGE_BEITRAEGE = 'vertraege_beitraege';
    
    public static $VEREIN = 'verein';
    public static $VEREIN_BANKDATEN = 'verein_bankdaten';
    
    public static $SETTING = 'setting';
    public static $SETTING_USER = 'setting_user';
    public static $SETTING_USER_RIGHT = 'setting_user_right';
    public static $SETTING_ROLES = 'setting_right';
    
    public function __construct($id='') {
        if($id!='') {
            $temp = Database::getDefault()->openId(Permission::$TABLENAME,$id,Permission::$CLASSNAME);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
        }
    }

    /**
     * get all availebale permissions
     * 
     * @return {array} Array of Permission objects
     */
    public static function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM ".$VEREIN['prefix'].Permission::$TABLENAME;
        return Database::getDefault()->execute($query, Permission::$CLASSNAME);
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function getDescription() {
        return $this->description;
    }

    /**
     * create table for banking contacts or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>update / create " . Permission::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . Permission::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Permission::$TABLENAME . " ADD COLUMN name VARCHAR(50) NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . Permission::$TABLENAME . " ADD COLUMN description TEXT DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "INSERT IGNORE INTO " . $VEREIN['prefix'] . Permission::$TABLENAME . " SET name=?";
        $reflection = new ReflectionClass(Permission::$CLASSNAME);
        $static = $reflection->getStaticProperties();
        foreach($static as $key=>$value) {
            if($value == Permission::$TABLENAME || $value == Permission::$CLASSNAME) {
                continue;
            }
            echo $value."<br />";
            Database::getDefault()->executePrepare($query, array($value=>"s"));
        }
        echo "<br />";
    }
}
?>